package com.duolebo.uteped_sdk.msg;

import androidx.annotation.NonNull;

import com.duolebo.uteped_sdk.UTEPedEventHandler;
import com.duolebo.uteped_sdk.utils.Const;
import com.yc.nadalsdk.bean.MotionGoalConfig;
import com.yc.nadalsdk.bean.Response;
import com.yc.nadalsdk.bean.TimeDisplay;
import com.yc.nadalsdk.constants.LanguageType;

import java.util.ArrayList;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
 * @Author: LiuYang
 * @Date: 2023/12/12 14:42
 * @Description:
 */
public class SpaceMessage extends BaseMessage {
    private final static String PATH = "space";

    private final static String M_resetFactory = "resetFactory";
    private final static String M_setTemperatureUnit = "setTemperatureUnit";
    private final static String M_setLengthUnits = "setLengthUnits";
    private final static String M_setWeightUnits = "setWeightUnits";
    private final static String M_setLanguage = "setLanguage";
    private final static String M_setMotionGoal = "setMotionGoal";
    private final static String M_syncWorkoutHistoryData = "syncWorkoutHistoryData";


    /**
     * BaseCallback
     *
     * @param methodChannel methodChannel
     * @param eventHandler  eventHandler
     */
    public SpaceMessage(MethodChannel methodChannel, UTEPedEventHandler eventHandler) {
        super(methodChannel, eventHandler);
    }

    @Override
    public boolean methodCallConsumed(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        if (!call.hasArgument(Const.KEY_METHOD))
            return false;

        if (super.methodCallConsumed(call, result))
            return true;

        boolean consumed = true;
        String method = call.argument(Const.KEY_METHOD);
        Response<?> response;
        boolean success = false;
        String errorCode = "";

        assert method != null;
        switch (method) {
            case M_resetFactory:
                if (uteBleClient.isConnected()) {
                    response = uteBleClient.getUteBleConnection().resetFactory(0);
                    success = response.isSuccess();
                }
                result.success(success);
                break;
            case M_setTemperatureUnit:
                if (uteBleClient.isConnected()) {
                    response = uteBleClient.getUteBleConnection().setTemperatureUnit(call.argument(Const.KEY_VALUE));
                    errorCode = String.valueOf(response.getErrorCode());
                }
                result.success(errorCode);
                break;
            case M_setLengthUnits:
                if (uteBleClient.isConnected()) {
                    response = uteBleClient.getUteBleConnection().setLengthUnits(call.argument(Const.KEY_VALUE));
                    errorCode = String.valueOf(response.getErrorCode());
                }
                result.success(errorCode);
                break;
            case M_setWeightUnits:
                if (uteBleClient.isConnected()) {
                    response = uteBleClient.getUteBleConnection().setWeightUnit(call.argument(Const.KEY_VALUE));
                    errorCode = String.valueOf(response.getErrorCode());
                }
                result.success(errorCode);
                break;
            case M_setLanguage:
                if (uteBleClient.isConnected()) {
                    int language = call.argument(Const.KEY_LANGUAGE);
                    int units = call.argument("units");
                    response = uteBleClient.getUteBleConnection().setLanguage(language, units);
                    errorCode = String.valueOf(response.getErrorCode());
                }
                result.success(errorCode);
                break;
            case M_setMotionGoal:
                if (uteBleClient.isConnected()) {
                    Map<String, Integer> config = call.argument(Const.KEY_VALUE);
                    assert config != null;
                    if(!config.containsKey("step")
                            || !config.containsKey("calorie")
                            || !config.containsKey("distance")
                            || !config.containsKey("duration")) {
                        result.success(-1);
                        return false;
                    }
                    MotionGoalConfig motionGoalConfig = new MotionGoalConfig();
                    motionGoalConfig.setGoalStep(config.get("step"));
                    motionGoalConfig.setGoalCalorie(config.get("calorie"));
                    motionGoalConfig.setGoalDistance(config.get("distance"));
                    motionGoalConfig.setGoalDuration(config.get("duration"));
                    response = uteBleClient.getUteBleConnection().setMotionGoal(motionGoalConfig);
                    errorCode = String.valueOf(response.getErrorCode());
                }
                result.success(errorCode);
                break;
            case M_syncWorkoutHistoryData:
                if (uteBleClient.isConnected()) {
                    Map<String, Integer> timeMap = call.argument(Const.KEY_VALUE);
                    assert timeMap != null;
                    if(!timeMap.containsKey("startTime")
                            ||!timeMap.containsKey("endTime")) {
                        result.success(-1);
                        return false;
                    }
                    d(timeMap.toString());
                    int startTime = timeMap.get("startTime");
                    int enTime = timeMap.get("endTime");

                    response = uteBleClient.getUteBleConnection().syncWorkoutHistoryData(startTime, enTime);
                    result.success(response.getData());
                }
                else {
                    result.success(new ArrayList<>());
                }

                break;

            default:
                consumed = false;
        }

        return consumed;
    }

    @Override
    public String getPath() {
        return PATH;
    }
}
